package com.neusoft.weekreport.component.bizService.impl;

import com.neusoft.weekreport.component.bizService.RoleRefMenuBizService;
import com.neusoft.weekreport.component.entities.BgMenu;
import com.neusoft.weekreport.component.entities.BgMenuExample;
import com.neusoft.weekreport.component.entities.BgRoleRefMenu;
import com.neusoft.weekreport.component.entities.BgRoleRefMenuExample;
import com.neusoft.weekreport.component.req.RoleRefMenuAddReq;
import com.neusoft.weekreport.component.req.RoleRefMenuByRoleIdReq;
import com.neusoft.weekreport.component.resp.GetMenuInfoListResp;
import com.neusoft.weekreport.mapper.BgMenuMapper;
import com.neusoft.weekreport.mapper.BgRoleRefMenuMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class RoleRefMenuServiceImpl implements RoleRefMenuBizService {

    @Resource
    BgRoleRefMenuMapper bgRoleRefMenuMapper;
    @Resource
    BgMenuMapper bgMenuMapper;

    @Override
    public GetMenuInfoListResp menuInfoListByRoleId(RoleRefMenuByRoleIdReq req) throws Exception {
        GetMenuInfoListResp resp = new GetMenuInfoListResp();
        BgRoleRefMenuExample bgRoleRefMenuExample = new BgRoleRefMenuExample();
        BgRoleRefMenuExample.Criteria criteria = bgRoleRefMenuExample.createCriteria();
        criteria.andRoleIdEqualTo(req.getRoleId());
        List<BgRoleRefMenu> bgRoleRefMenuList =bgRoleRefMenuMapper.selectByExample(bgRoleRefMenuExample);
        List<BgMenu> bgMenuList = new ArrayList<>();
        if(!CollectionUtils.isEmpty(bgRoleRefMenuList)){
            List<String> list = new ArrayList<>();
            for (BgRoleRefMenu bgRoleRefMenu : bgRoleRefMenuList) {
                list.add(bgRoleRefMenu.getMenuId());
            }
            BgMenuExample bgMenuExample = new BgMenuExample();
            BgMenuExample.Criteria criteria1 = bgMenuExample.createCriteria();
            criteria1.andEnableEqualTo(1);
            criteria1.andIfShowEqualTo(1);
            criteria1.andIdIn(list);
            bgMenuList = bgMenuMapper.selectByExample(bgMenuExample);
        }
        resp.setMenuInfos(bgMenuList);
        return resp;
    }

    @Override
    @Transactional

    public void addRoleRefMenuByRoleIdAndMenuId(RoleRefMenuAddReq req) throws Exception {
        BgRoleRefMenuExample bgRoleRefMenuExample = new BgRoleRefMenuExample();
        BgRoleRefMenuExample.Criteria criteria = bgRoleRefMenuExample.createCriteria();
        criteria.andRoleIdEqualTo(req.getRoleId());
        bgRoleRefMenuMapper.deleteByExample(bgRoleRefMenuExample);
        BgRoleRefMenu bgRoleRefMenu = new BgRoleRefMenu();
        bgRoleRefMenu.setRoleId(req.getRoleId());
        List<String> menuIdList = req.getPermissionIdList();
        for (String menuId : menuIdList) {
            bgRoleRefMenu.setMenuId(menuId);
            bgRoleRefMenuMapper.insertSelective(bgRoleRefMenu);
        }
    }
}
